$(function () {
  const form = layui.form;
  const layer = layui.layer;
  form.verify({
    nikename: function (value) {
      if (value.length > 6) return "昵称长度必须在1~6之间";
    },
  });
  initUserInfo();
  function initUserInfo() {
    $.ajax({
      url: "/my/userinfo",
      method: "get",
      success(res) {
        if (res.status !== 0) return layer.msg(res.message);
        console.log(res);
        form.val("formUserInfo", res.data);
      },
    });
  }
  $("#btnReset").on("click", function (e) {
    e.preventDefault();
    initUserInfo();
  });
  $(".layui-form").on("submit", function (e) {
    e.preventDefault();
    $.ajax({
      url: "/my/userinfo",
      method: "POST",
      data: $(this).serialize(),
      success(res) {
        if (res.status !== 0) return layer.msg(res.message) || "更新失败";
        layer.msg(res.message);
        window.parent.getUserInfo();
      },
    });
  });
});
